//
//  DRMineViewController.m
//  DoctorRen
//
//  Created by Lemon on 2018/11/19.
//

#import "DRMineViewController.h"
#import "DRMineCell.h"
#import "DRMineDataModel.h"
#import "DRMineModel.h"

@interface DRMineViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
    
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *titleArray;

@property (nonatomic, strong) NSArray *imageArray;

@property (nonatomic, strong) DRMineModel *mineModel;
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
@property (weak, nonatomic) IBOutlet UILabel *cookcountLB;
@property (weak, nonatomic) IBOutlet UILabel *collectionLabel;
@property (weak, nonatomic) IBOutlet UILabel *commentsLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIView *releaseView;
@property (weak, nonatomic) IBOutlet UIView *collectView;

@property (weak, nonatomic) IBOutlet UIView *commentView;

@end

@implementation DRMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.avatarImageView.layer.cornerRadius = 30;
    self.avatarImageView.layer.masksToBounds = YES;
    
    self.titleArray = @[@"我的好友",@"邀请好友",@"设备管理",@"食物黑名单",@"健康档案"];
    self.imageArray = @[@"我的_我的好友",@"我的_邀请好友",@"我的_设备管理",@"我的_食物黑名单",@"我的_健康档案"];
    [self setupUI];
    [SVProgressHUD showWithStatus:@"正在加载"];
    [self loadData];
    DRUserModel *model = [DRLoginDataModel getUserModel];
    self.nameLabel.text = model.name ? : @"未登录";
    [self.avatarImageView setImageWithURLString:model.pic_id];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [self loadData];
    [self setKefuButtonHidden:NO];
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

-(void) setupUI{
    self.avatarImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapAvatar = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoUserInfo)];
    [self.avatarImageView addGestureRecognizer:tapAvatar];
    
    UITapGestureRecognizer *tapA = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoRelease)];
    [self.releaseView addGestureRecognizer:tapA];
    UITapGestureRecognizer *tapB = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoCollect)];
    [self.collectView addGestureRecognizer:tapB];
    UITapGestureRecognizer *tapC = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoComment)];
    [self.commentView addGestureRecognizer:tapC];
    
}
    
-(void) loadData{
    [DRMineDataModel loadMineDataWithSuccess:^(DRMineModel * _Nonnull model) {
        self.mineModel = model;
        self.nameLabel.text = self.mineModel.name;
        [self.avatarImageView setImageWithURLString:model.pic_id];
        self.cookcountLB.text = self.mineModel.cookcout;
        self.collectionLabel.text= self.mineModel.collection;
        self.commentsLabel.text = self.mineModel.comments;
        DRUserModel *user = [DRLoginDataModel getUserModel];
        user.name = model.name;
        [DRLoginDataModel saveUserModel:user];
        [SVProgressHUD dismiss];
    } andFail:^(NSError *error) {
        if (error.code == 500) {
            [SVProgressHUD dismiss];
            WEAKSELF
            if (![DRLoginDataModel isLogin]) {
                return ;
            }
            [DRLoginDataModel setIsLogin:NO];
            [self presentAlertViewWithTitle:@"请重新登录" message:@"" confirmTitle:@"确定" handler:^{
                [weakSelf gotoLogin];
            }];
        }else{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            
        }
    }];
}

-(void) gotoUserInfo{
    if (self.mineModel.info_url) {
        [self pushWebViewWithUrl:self.mineModel.info_url];
    }else{
        [self presentNeedLogin:YES];
    }
}

-(void) gotoCollect{
    if (self.mineModel.collect_url) {
        [self pushWebViewWithUrl:self.mineModel.collect_url];
    }else{
        [self presentNeedLogin:YES];
    }
}

-(void) gotoRelease{
    if (self.mineModel.healthylist_url) {
        [self pushWebViewWithUrl:self.mineModel.healthylist_url];
    }else{
        [self presentNeedLogin:YES];
    }

}

-(void) gotoComment{
    if (self.mineModel.pinlun_url) {
        [self pushWebViewWithUrl:self.mineModel.pinlun_url];
    }else{
        [self presentNeedLogin:YES];
    }

}

- (IBAction)clickSettingButton:(id)sender {
    if ([DRLoginDataModel getUserToken].length == 0) {
        [self presentNeedLogin:YES];
        return;
    }
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    [self.navigationController pushViewController:[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DRSettingViewController"] animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    [self setKefuButtonHidden:YES];
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    DRMineCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DRMineCell" forIndexPath:indexPath];
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.imageIV.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
    return cell;
}
    
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.mineModel) {
        [self presentNeedLogin:YES];
        return;
    }
    switch (indexPath.row) {
        case 0:
            [self pushWebViewWithUrl:self.mineModel.addfriend_url];
            break;
        case 1:
            [self pushWebViewWithUrl:self.mineModel.getfriend_url];
            break;
        case 2:
        {
            if ([DRLoginDataModel getUserToken].length == 0) {
                [self presentNeedLogin:YES];
                return;
            }
            UIViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DRDevicesVC"];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            self.hidesBottomBarWhenPushed = NO;
            [self setKefuButtonHidden:YES];
        }
            break;
//        case 3:
//            [self pushWebViewWithUrl:self.mineModel.eatresult_url];
//            break;
        case 3:
            [self pushWebViewWithUrl:self.mineModel.backlist_url];
            break;
        case 4:
            [self pushWebViewWithUrl:self.mineModel.selftest_url];
            break;
        default:
            break;
    }
}


    
@end
